package com.shopping.Service.ServiceImpl;

import com.shopping.Entity.CommentInfo;
import com.shopping.Entity.UserInfo;
import com.shopping.Entity.UserScore;
import com.shopping.Service.CommentInfoService;
import com.shopping.Service.CommentService;
import com.shopping.Service.UserInfoService;
import com.shopping.Service.UserScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentInfoService commentInfoService;
    @Autowired
    UserScoreService userScoreService;
    @Autowired
    UserInfoService userInfoService;
    @Override
    public boolean commentSubmit(CommentInfo commentInfo, UserInfo userInfo) {
        //规则有2
        //每条评论增加10积分
        //已有评论补充的不加分
        //根据commentInfo的id查询历史是否有过评论
        boolean flag = commentInfoService.isComment(commentInfo.getId());
        if(!flag){
            //没有信息,存新
            commentInfo.setCreateTime((Timestamp) new Date());
            commentInfo.setUserid(userInfo.getId());
            //存comment,并返回commentid
            CommentInfo commentInfo1 = commentInfoService.save(commentInfo);
            if (commentInfo1 != null){
                int kindid = commentInfo.getId();
                UserScore userScore = new UserScore(0,userInfo.getId(),2,kindid,10, (Timestamp) new Date());
                //存积分
                userScoreService.save(userScore);
                userInfo.setSumscore(userInfo.getSumscore()+10);
                userInfoService.updateUserInfo(userInfo);
                return true;
            }
            return false;
        }else {
            //有信息,更新内容,历史信息是否保留由调用端判断,此处直接覆盖
            //另有追评相关信息,数据库表无对应设计,无法实现.
            //update commentinfo
            commentInfoService.updateCommentInfo(commentInfo);
            return false;
        }
    }
}
